<?php

namespace app\admin\controller;

use think\Controller;

use think\Db;
use think\Request;
use app\admin\model\Banner as BannerModel;


//轮播图管理

class Banner extends Common
{
//轮播列表
  public function index()
    {	
      return $this->fetch();

    }

    public function lst()
    { 

      $req=input("post.");
      $get=input("get.");

      $page=$req['page'];  //当前页   1

      $limit=$req['limit']; //每页几行数据  10

      $begin=($page-1)*10;   //0,10  10,10 
      $where = [];
      if (isset($req['time']) ) {
          if (!empty($req['time'])){
              $arr = explode('~',$req['time']);
              $arr[0] = strtotime($arr[0]);
              $arr[1] = strtotime($arr[1])+24*3600;
              $where['create_time'] = ['between',$arr];
          }
      }
      if (isset($req['url']) ) {
          if (!empty($req['url'])){
              $where['url']=['like','%'.$req['url'].'%'];
          }
      }
      $count=BannerModel::where($where)->count();
      $data=[];
      $list=BannerModel::order('id', 'desc')->where($where)->paginate($limit,false,['page'=>$page])->all();
      

        $data['code']=0;

        $data['data']=$list;

        $data['msg']="返回成功";

        $data['count']=$count;

        echo json_encode($data);

    }

    public function add(){
      $get=input("get.");
      if(Request::instance()->isPost()){
        $post=input('post.');
        $post['addtime']=time();
        $banner=new BannerModel($post);
        $num=$banner->allowField(true)->save();
        if($num>0){
          return ['code'=>1,'msg'=>'添加成功','data'=>''];
        }else{
          return ['code'=>0,'msg'=>'添加失败','data'=>''];
        }
      }
      return $this->fetch();
    }
    public function update(){
      $id=input("get.id");
      if(Request::instance()->isPost()){
        $post=input('post.');
        unset($post['image']);
        $banner = new BannerModel();
        $num=$banner->allowField(true)->save($post,['id' => $id]);
        // $num=Db::table('quan')->where('id',$id)->update(['name'=>$post['name'],'addtime'=>time()]);
        if($num>0){
          return ['code'=>1,'msg'=>'修改成功','data'=>''];
        }else{
          return ['code'=>1,'msg'=>'修改成功','data'=>''];
        }
      }
      $data=BannerModel::get($id);
      $this->assign("data",$data);
      return $this->fetch();
    }

    //删除轮播

    public function remove()
    {	
    	$data=input("post.");
      $admin=BannerModel::where('id',"in",$data['id'])->select();
      $num=BannerModel::destroy($data['id']);
       if($num>0){
        for ($i=0; $i <count($admin) ; $i++) { 
            if(!empty($admin[$i]['path'])){
            $path=ltrim('/',$admin[$i]['path']);
            //删除之前图片
            if(file_exists($path)){
              unlink($path);
            }
          }
        }
      } 
      if($num){
          return ['code'=>1,'msg'=>'删除成功','data'=>''];
      }else{
        return ['code'=>0,'msg'=>'删除失败','data'=>''];
      }
    }

    //修改状态

    public function up_status()

    { 

       $id=input("get.id");

       $data=Db::table('banner')->where('id',$id)->find();

       $status=$data['status'];

       if($status==1){  //当前启用

          $num=Db::table('banner')->where('id',$id)->update(['status'=>2]);

          if($num>0){

               return 1;

          }else{

               return 2;

          }

      }else{

          $num=Db::table('banner')->where('id',$id)->update(['status'=>1]);

          if($num>0){

               return 3;

          }else{

               return 4;

          }

       }

    }

     //文件上传

    public function upload(){
      $file = request()->file('image');
      $path='banner';
      return $this->uploads($file,$path);

	 }
//文件上传
    public function uploads($file,$path){
        $return=[];
            // 获取表单上传文件 例如上传了001.jpg
            // $file = request()->file('image');
            // 移动到框架应用根目录/public/uploads/ 目录下
            if($file){
                // $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads'.DS.'admin');
                $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads'.DS.$path);
                if($info){
                  $filename='uploads/banner/'.str_replace('\\','/',$info->getSaveName());
                  $img=new \Img($filename);
                  if($img->getimg($filename,300)){
                    $da['msg']='上传成功';
                    // 成功上传后 获取上传信息
                    $return['code']=0;
                    $return['data']='/uploads/banner/'.str_replace('\\','/',$info->getSaveName());
                  }else{
                    $da['msg']='上传失败';
                    $da['data']='上传压缩失败';
                    $da['code']=1;
                  }
                    
                }else{
                    // 上传失败获取错误信息
                    $return['code']=1;
                    // $return['msg']="上传成功";
                    $return['msg']="上传失败";
                    $return['data']=$file->getError();
                }
            }
        return json_encode($return);
    }


}

